package com.wanghaixin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanghaixin.dao.CarMakeMapper;
import com.wanghaixin.dto.DataGridResult;
import com.wanghaixin.dto.QueryDTO;
import com.wanghaixin.pojo.CarMake;
import com.wanghaixin.pojo.CarMakeExample;
import com.wanghaixin.service.CarMakeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.xml.crypto.Data;
import java.util.List;

/**
 * @Auther: 王海新
 * @Date: 2020/11/2 07:53
 * @Description: 汽车品牌服务层开发
 */
@Service
public class CarMakeServiceImpl implements CarMakeService {

    @Autowired
    private CarMakeMapper carMakeMapper;

    @Override
    public int addCarMake(CarMake carMake) {
        int i = carMakeMapper.insertSelective(carMake);
        return i;
    }

    @Override
    public void delCarMake(Integer id) {
        int i = carMakeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateCarMake(CarMake carMake) {
        int update = carMakeMapper.updateByPrimaryKeySelective(carMake);
        return update;
    }

    @Override
    public CarMake finfById(Integer id) {
        CarMake carMake = carMakeMapper.selectByPrimaryKey(id);
        return carMake;
    }

    @Override
    public DataGridResult findByPage(QueryDTO queryDTO) {
        PageHelper.offsetPage(queryDTO.getOffset(),queryDTO.getLimit());

        CarMakeExample example = new CarMakeExample();

        String sort = queryDTO.getSort();
        if (!StringUtils.isEmpty(sort)) {
            example.setOrderByClause("id");
        }

        List<CarMake> carMakes = carMakeMapper.selectByExample(example);
        PageInfo<CarMake> pageInfo = new PageInfo<>(carMakes);
        long total = pageInfo.getTotal();
        DataGridResult dataGridResult = new DataGridResult(total, carMakes);
        return dataGridResult;
    }

    @Override
    public List<CarMake> findAll() {
        List<CarMake> carMakes = carMakeMapper.selectByExample(null);
        return carMakes;
    }
}
